﻿using CemsTransfer.BUS;
using CemsTransfer.Event;
using CemsTransfer.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO.Pipelines;

namespace CemsTransfer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class NetWorkController : ControllerBase
    {
        public NetWorkController()
        {

        }
        /// <summary>
        /// 查询网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNetInfo")]
        public ResponseMessage QueryNetInfo(NetInfo netInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetWorkBUS netWorkBUS = new NetWorkBUS();
                NetInfo net = new NetInfo();
                List<NetInfo> line18NetInfos = netWorkBUS.GetNetInfo(CemsDataProcess.NetWorkNum);
                List<NetInfo> line18NetInfo1 = line18NetInfos.FindAll(delegate (NetInfo e) { return e.ID == netInfo.ID; });
                if (line18NetInfo1 != null && line18NetInfo1.Count > 0)
                {
                    net = line18NetInfo1[0];
                }
                else
                {
                    net.ID = netInfo.ID;
                }
                message.errcode = 0;

                message.data = net;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网络信息
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNetInfo")]
        public ResponseMessage UpdateNetInfo(NetInfo netInfo)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                string value = string.Empty;
                string[] array = netInfo.Mask.Replace(" ", "").Split('.');
                for (int i = 0; i < array.Length; i++)
                {
                    value += Convert.ToString(Convert.ToInt32(array[i]), 2);
                }
                value = value.PadRight(32, '0');

                string keyWord = "1";
                int index = 0;
                int count = 0;
                while ((index = value.IndexOf(keyWord, index)) != -1)
                {
                    count++;
                    index = index + keyWord.Length;
                }
                netInfo.Mask = count.ToString();

                NetWorkBUS netWorkBUS = new NetWorkBUS();
                bool result = netWorkBUS.UpdateLine18NetInfo(netInfo, CemsDataProcess.NetWorkNum);
                if (result)
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "修改失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 查询网口数量
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryNetNum")]
        public ResponseMessage QueryNetNum()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                message.errcode = 0;
                message.data = Convert.ToInt32(AppSettingsHelper.Configuration["NetWorkNum"]);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "查询网络信息异常";
            }

            return message;
        }

        /// <summary>
        /// 修改网口数量
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateNetNum")]
        public ResponseMessage UpdateNetNum(int num)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {

                string contentPath = AppDomain.CurrentDomain.BaseDirectory;   //项目根目录
                var filePath = contentPath + "appsettings.json";
                JObject jsonObject;
                using (StreamReader file = new StreamReader(filePath))
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    jsonObject = (JObject)JToken.ReadFrom(reader);
                    jsonObject["NetWorkNum"] = num;
                    CemsDataProcess.NetWorkNum = num;
                }

                using (var writer = new StreamWriter(filePath))
                using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
                {
                    jsonObject.WriteTo(jsonwriter);
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "更新异常";
            }

            return message;
        }

        /// <summary>
        /// 设备重启
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("Reboot")]
        public ResponseMessage Reboot()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                NetWorkBUS netWorkBUS = new NetWorkBUS();
                netWorkBUS.RunCommand("reboot");
                message.errcode = 0;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "设备重启异常";
            }

            return message;
        }
    }
}
